package com.ce.common.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.text.DecimalFormat;

import static android.content.Context.TELEPHONY_SERVICE;

/**
 * Created by LiLongHui on 2017/7/21.
 */

public class AppUtil {
    private final static String CACHE_FILE = "PuGuTang";

    public static int getVersionCode(Context context) {
        int versionCode = -1;
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo packInfo = manager.getPackageInfo(context.getPackageName(), 0);
            versionCode = packInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    //版本号
    public static String getVersionName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);

            return packageInfo.versionName;

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    //包名
    public static String getPackageName(Context context) {
        try {
            return context.getPackageName();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //mac
    public static String getMac() {
        String macSerial = null;
        String str = "";
        try {
            Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address ");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);

            for (; null != str; ) {
                str = input.readLine();
                if (str != null) {
                    macSerial = str.trim();// 去空格
                    break;
                }
            }
        } catch (IOException ex) {
            // 赋予默认值
            ex.printStackTrace();
        }
        return macSerial;
    }

    //屏幕宽度
    public static int getWidthToInt(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return  outMetrics.widthPixels;
    }

    //屏幕宽度
    public static String getWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return "" + outMetrics.widthPixels;
    }

    //屏幕宽度
    public static String getHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return "" + outMetrics.heightPixels;
    }

    //    //IMEI
    public static String getImei(Context context) {
        String imei = ((TelephonyManager) context.getSystemService(TELEPHONY_SERVICE))
                .getDeviceId();
        return imei;

    }

    //Android_id
    public static String getAndroidId(Context context) {
        String android_id = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        return android_id;
    }

    //    //IMSI
    public static String getIMSI(Context context) {
        TelephonyManager mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return mTelephonyMgr.getSubscriberId();
    }

    /**
     * 返回SD卡根路径
     *
     * @return
     */
    public static String getSdcardRoot(Context context) {

        String retDir = "";
        try {
            File dir = isSdcardReady() ? Environment.getExternalStoragePublicDirectory(CACHE_FILE) : context.getExternalFilesDir(CACHE_FILE);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            retDir = dir.getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
            retDir = "";
        }
        return retDir;
    }

    /**
     * 判断SD卡是否存在
     *
     * @return
     */
    public static boolean isSdcardReady() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    public static long getDirSize(File dir) {
        if (dir == null) {
            return 0;
        }
        if (!dir.isDirectory()) {
            return 0;
        }
        long dirSize = 0;
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isFile()) {
                dirSize += file.length();
            } else if (file.isDirectory()) {
                dirSize += file.length();
                dirSize += getDirSize(file); // 如果遇到目录则通过递归调用继续统计
            }
        }

        return dirSize;
    }

    public static double getDiskCacheSize(Context context, String cacheDir) {
        try {
            String rootPath = getSdcardRoot(context);
            File file = new File(rootPath, cacheDir);
            long dirSize = getDirSize(file);

            double size = 0;
            size = (dirSize + 0.0) / (1024 * 1024);
            DecimalFormat df = new DecimalFormat("0.0");// 以Mb为单位保留两位小数
            double filesize = Double.valueOf(df.format(size));
            return filesize;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 验证手机号是否合法
     * @param phone 要验证的手机号
     * @return
     */
    public static boolean checkPhoneAvailable( String phone) {
        if (!TextUtils.isEmpty(phone)&&phone.replaceAll(" ","").length()==11){
            return true;
        }
        return false;
    }

}
